<?php


namespace app\api\controller;


use app\api\model\InfoTypeRecord;
use app\api\model\UserAdviseDataModel;
use app\api\model\UserAdviseModel;
use app\api\model\UserFoodAttrModel;
use app\api\model\UserFoodModel;
use app\api\model\UserInfoModel;
use app\api\model\UserSportModel;
use app\api\model\UserTypeDataModel;
use app\api\model\UserTypeModel;
use app\common\controller\Api;

class RecordAdminData extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $day = [
        '99' => '全部',
        '2' => '饮食',
        '3' => '热量',
        '4' => '血压',
        '5' => '饮水',
        '7' => '心率',
        '6' => '步数',
        '8' => '运动时间',
        '10' => '卡路里',
        '11' => '运动距离',
        '12' => '睡眠'
    ];

    protected $week = [
        '99' => '全部',
        '13' => '入睡时间',
        '1' => '睡眠时间',
        '3' => '血压',
        '4' => '血氧',
        '5' => '运动步数',
        '6' => '总饮水量',
        '7' => '大便次数',
        '8' => '总热量',
        '9' => '总蛋白质',
        '10' => '总脂肪',
        '11' => '总膳食纤维',
        '12' => '总碳水化合物'
    ];

    /*
     * 修改数据建议
     */
    public function editAdvise()
    {
        $user_id = input('user_id');
        $type_id = input('type_id');
        $type = input('type');//0=日统计 1=周统计
        $title = input('title');//0=日统计 1=周统计

        if(empty($user_id) || empty($type_id) || empty($title)){
            $this->error('缺少请求参数');
        }

        $userType = UserTypeModel::get(['pid'=>$type_id,'type'=>$type]);

        if(!$userType){
            $this->error('请求参数错误');
        }

        $check = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

        if(!$check){
            $data = [
                'user_id'=>$user_id,
                'type_id'=>$userType['id'],
                'title'=>$title,
            ];
            $model = new UserAdviseModel();
            $model->save($data);
        }else{
            $data = [
                'title'=>$title,
            ];
            $check->save($data);
        }

        $this->success('修改成功');
    }

    /*
     * 获取参考值列表
     */
    public function typeDataList()
    {
//        $user_id = input('user_id');
        $type = input('type');

        if(!in_array($type,[0,1])){
            $this->error('请求参数错误');
        }

        $list = UserTypeDataModel::all(['type'=>$type]);

        $this->success('请求成功',$list);

    }

    /*
     * 设置用户对比数据
     */
    public function editUserDataInfo()
    {
        $user_id = input('user_id');
        $type_id = input('type_id');
        $num = input('num');
        if(empty($user_id) || empty($type_id) || empty($num)){
            $this->error('缺少请求参数');
        }

        $check = UserAdviseDataModel::get(['user_id'=>$user_id,'type_id'=>$type_id]);

        if(!$check){
            $data = [
                'user_id'=>$user_id,
                'type_id'=>$type_id,
                'num'=>$num,
            ];
            $model = new UserAdviseDataModel();
            $model->save($data);
        }else{
            $data = [
                'num'=>$num,
            ];
            $check->save($data);
        }

        $this->success('修改成功');

    }

    /*
     * 日数据统计
     */
    public function dayRecord()
    {

        $day = input('post.day', date('Y-m-d', time()));

        $user_id = input('user_id');
        $type = input('type');

        $data = [];

        //三餐食物热量比
        $foodModel = new UserFoodModel();

        $userTypeModel = new UserTypeModel();
        $zao = $foodModel::where(['day' => $day, 'user_id' => $user_id, 'type' => 1])->value('hot');
        $zhong = $foodModel::where(['day' => $day, 'user_id' => $user_id, 'type' => 2])->value('hot');
        $wan = $foodModel::where(['day' => $day, 'user_id' => $user_id, 'type' => 3])->value('hot');

        $userType = $userTypeModel::get(['pid'=>13,'type'=>0]);

        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '热量比';

        $sum_san_hot = $zao + $zhong + $wan;
        if ($sum_san_hot > 0) {
            $zao_b = number_format($zao / $sum_san_hot, 2) * 100;
            $zhong_b = number_format($zhong / $sum_san_hot, 2) * 100;
            $wan_b = number_format($wan / $sum_san_hot, 2) * 100;
            $data[] = [
                'title' => [
                    'text' => '三餐食物热量比',
                    'subtext' => $text,
                    'left' => 'center',
                ],
                'tooltip' => [
                    'trigger' => 'item'
                ],
                'legend' => [
                    'orient' => 'vertical',
                    'left' => 'left',
                ],
                'series' => [
                    [
                        'name' => '热量比',
                        'type' => 'pie',
                        'radius' => '50',
                        'data' => [
                            [
                                'value' => $zao_b,
                                'name' => '早餐比例',
                            ],
                            [
                                'value' => $zhong_b,
                                'name' => '午餐比例',
                            ],
                            [
                                'value' => $wan_b,
                                'name' => '晚餐比例',
                            ]
                        ],
                        'emphasis' => [
                            'itemStyle' => [
                                'shadowBlur' => 10,
                                'shadowOffsetX' => 10,
                                'shadowColor' => 'rgba(0, 0, 0, 0.5)'
                            ]
                        ]
                    ]
                ]
            ];
        }


        //三大产能营养比例

        $s_fat = $foodModel::where(['day' => $day, 'user_id' => $user_id])->sum('fat');
        $s_water = $foodModel::where(['day' => $day, 'user_id' => $user_id])->sum('water');
        $s_protein = $foodModel::where(['day' => $day, 'user_id' => $user_id])->sum('protein');
        $sum_san_ying = $s_fat + $s_water + $s_protein;
        $userType = $userTypeModel::get(['pid'=>14,'type'=>0]);

        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '营养比例';

        if ($sum_san_ying > 0) {
            $fat_b = number_format($s_fat / $sum_san_ying, 2) * 100;
            $water_b = number_format($s_water / $sum_san_ying, 2) * 100;
            $protein_b = number_format($s_protein / $sum_san_ying, 2) * 100;
            $data[] = [
                'title' => [
                    'text' => '三大产能营养比例',
                    'subtext' => $text,
                    'left' => 'center',
                ],
                'tooltip' => [
                    'trigger' => 'item'
                ],
                'legend' => [
                    'orient' => 'vertical',
                    'left' => 'left',
                ],
                'series' => [
                    [
                        'name' => '营养比例',
                        'type' => 'pie',
                        'radius' => '50',
                        'data' => [
                            [
                                'value' => $water_b,
                                'name' => '碳水化合物',
                            ],
                            [
                                'value' => $fat_b,
                                'name' => '脂肪',
                            ],
                            [
                                'value' => $protein_b,
                                'name' => '蛋白质',
                            ]
                        ],
                        'emphasis' => [
                            'itemStyle' => [
                                'shadowBlur' => 10,
                                'shadowOffsetX' => 10,
                                'shadowColor' => 'rgba(0, 0, 0, 0.5)'
                            ]
                        ]
                    ]
                ]
            ];
        }

        if (empty($type)) {
            $search = $this->day;
        } else {
            $search_arr = explode(',', $type);
            $search = [];
            $day_s = $this->day;
            foreach ($search_arr as $key => $val) {
                if ($val == 99) {
                    $search = $this->day;
                    break;
                }
                $search[$key] = $day_s[$val];
            }
        }

//        $typeList = InfoTypeModel::all(['id' => ['in', $type]]);
//var_dump($search);die;
        $recordModel = new InfoTypeRecord();
        $userAdviseData = new UserAdviseDataModel();
        foreach ($search as $val) {

            switch ($val) {
                case '饮食':
                    $s_dietary = $foodModel::where(['day' => $day, 'user_id' => $user_id])->sum('dietary');
                    if ($s_dietary >= 0) {
                        $userType = $userTypeModel::get(['pid'=>2,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自饮食打卡';

                        //总碳水(参考)
                        $c_water_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>1]);
                        $c_water = !empty($c_water_info['num']) ? $c_water_info['num'] : 10;
                        //总脂肪(参考)
                        $c_water_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>2]);
                        $c_zhifang = !empty($c_water_info['num']) ? $c_water_info['num'] : 20;
                        //总蛋白质(参考)
                        $c_water_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>3]);
                        $c_danbai = !empty($c_water_info['num']) ? $c_water_info['num'] : 10;
                        //总膳食纤维(参考)
                        $c_water_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>4]);
                        $c_qianwei = !empty($c_water_info['num']) ? $c_water_info['num'] : 10;
                        $data[] = [
                            'title' => [
                                'text' => $val . '(g)',
                                'subtext' => $text
                            ],
                            'legend' => [
                                'data' => [
                                    '标准值',
                                    '摄入量'
                                ]
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' => [
                                    '总碳水',//碳水
                                    '总脂肪',//脂肪
                                    '总蛋白质',//蛋白质
                                    '总膳食纤维'//膳食纤维
                                ]
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '标准值',
                                    'type' => 'bar',
                                    'data' => [
                                        round($c_water,2), round($c_zhifang,2), round($c_danbai,2), round($c_qianwei,2)
                                    ]
                                ],
                                [
                                    'name' =>
                                        '摄入量',
                                    'type' => 'bar',
                                    'data' => [

                                        round($s_water, 2),round($s_fat, 2), round($s_protein, 2), round($s_dietary, 2)
                                    ]
                                ],
                            ],
                            'toolbox' => [
                                'feature' => [
                                    'saveAsImage' => [
                                        'show' => true
                                    ],
                                    'toolbox' => [
                                        'show' => true
                                    ]
                                ]
                            ],
                            'animation' => false
                        ];

                    }
                    break;

                case '热量':
                    $s_hot = $foodModel::where(['day' => $day, 'user_id' => $user_id])->sum('hot');
                    $s_hot = sprintf("%.0f",$s_hot);
                    if ($s_hot >= 0) {
                        $userType = $userTypeModel::get(['pid'=>3,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '成人轻度体力劳动：2100-2400千卡；成人中度体力劳动：2300-2700千卡；成人重度体力劳动：2700-3200千卡';
                        //标准热量(参考)
                        $c_hot_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>5]);
                        $c_hot = !empty($c_hot_info['num']) ? $c_hot_info['num'] : 10;
                        $data[] = [
                            'xAxis' => [
                                'type' => 'category',
                                'data' => [
                                    '总热量',
                                    '标准热量'
                                ]
                            ],
                            'title' => [
                                'text' => $val . '(kcal)',
                                'subtext' => $text
                            ],
                            'tooltip' => [
                                'trigger' => 'axis',
                                'axisPointer' => [
                                    'type' => 'shadow'
                                ]
                            ],
                            'legend' => [
                                'data' => [
                                    '总热量',
                                    '标准热量'
                                ]
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'series' => [
                                [
                                    'data' => [
                                        $s_hot,
                                        [
                                            'value' => $c_hot,
                                            'itemStyle' => [
                                                'color' => '#a90000'
                                            ]
                                        ]
                                    ],
                                    'type' => 'bar'
                                ]
                            ]
                        ];
                    }

                    break;
                case '饮水':
                    //原来的逻辑 这是碳水合物的修改
                    //$s_water = UserFoodModel::where(['type' => 5, 'user_id' => $this->auth->id, 'day' => $day])->sum('water');
                    $water = \app\admin\model\UserFood::where(['day' => $day, 'user_id' => $this->auth->id, 'type' => 5])->column('id');
                    $water_zong = UserFoodAttrModel::where('pid', 'in', $water)->sum('food_value');
                    if ($water_zong >= 0) {
                        $userType = $userTypeModel::get(['pid'=>5,'type'=>0]);
                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);
                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '参考标准范围：1500-2500毫升';
                        //标准饮水(参考)
                        $c_water_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>10]);
                        $c_water = !empty($c_water_info['num']) ? $c_water_info['num'] : 10;

                        $data[] = [
                            'xAxis' => [
                                'type' => 'category',
                                'data' => [
                                    '总饮水',
                                    '标准饮水'
                                ]
                            ],
                            'title' => [
                                'text' => $val . '(ML)',
                                'subtext' => $text
                            ],
                            'tooltip' => [
                                'trigger' => 'axis',
                                'axisPointer' => [
                                    'type' => 'shadow'
                                ]
                            ],
                            'legend' => [
                                'data' => [
                                    '总饮水',
                                    '推荐饮水'
                                ]
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'series' => [
                                [
                                    'data' => [
                                        $s_water,
                                        [
                                            'value' => $c_water,
                                            'itemStyle' => [
                                                'color' => '#a90000'
                                            ]
                                        ]
                                    ],
                                    'type' => 'bar'
                                ]
                            ]
                        ];
                    }

                    break;
                case '步数':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 17])->order('taketime asc')->select();
                    $categories = [];
                    $series = [];
                    $b_g = [];
                    $b_d = [];
                    $bushu = 0;
                    foreach ($juliDetail as $v) {
                        if (!empty($v['type_value'])) {
                            $bushu += $v['type_value'];
                            $categories[] = date('H:i', $v['taketime']);
                            $series[] = $bushu;
                            $b_g[] = 10000;
                            $b_d[] = 6000;
                        }
                    }
                    if (!empty($series)) {
                        $userType = $userTypeModel::get(['pid'=>6,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '参考标准范围：6000-10000步';
                        $data[] = [
                            'title' => [
                                'text' => '步数',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $categories
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '步数',
                                    'data' =>
                                        $series,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准（高）',
                                    'data' =>
                                        $b_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准（低）',
                                    'data' =>
                                        $b_d,
                                    'type' => 'line'
                                ],
                            ]
                        ];
                    }

                    break;
                case '运动时间':
                    //$juliDetail = $recordModel->get(['user_id' => $this->auth->id, 'day' => $day, 'type_id' => 13]);
                    $juliDetail = UserSportModel::where(["user_id" => $this->auth->id, "day" => $day])->where('dakatime is not null')->field('endtime,starttime')->select();
                    $type_value = 0;
                    if (count($juliDetail) > 0) {
                        foreach ($juliDetail as $value) {
                            $time = ceil(($value['endtime'] - $value['starttime']) / 60);
                            $type_value += $time;
                        }
                    }
                    if ($type_value >= 0) {
                        $userType = $userTypeModel::get(['pid'=>8,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '参考标准范围：30-60分钟';
                        //标准饮水(参考)
                        $c_time_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>15]);
                        $c_time = !empty($c_time_info['num']) ? $c_time_info['num'] : 3000;

                        $data[] = [
                            'xAxis' => [
                                'type' => 'category',
                                'data' => [
                                    '总时间',
                                    '标准时间'
                                ]
                            ],
                            'title' => [
                                'text' => $val . '(min)',
                                'subtext' => $text
                            ],
                            'tooltip' => [
                                'trigger' => 'axis',
                                'axisPointer' => [
                                    'type' => 'shadow'
                                ]
                            ],
                            'legend' => [
                                'data' => [
                                    '总时间',
                                    '标准时间'
                                ]
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'series' => [
                                [
                                    'data' => [
                                        $type_value,
                                        [
                                            'value' => $c_time,
                                            'itemStyle' => [
                                                'color' => '#a90000'
                                            ]
                                        ]
                                    ],
                                    'type' => 'bar'
                                ]
                            ]
                        ];
                    }

                    break;
                case '心率':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 6])->order('taketime asc')->select();
                    $categories = [];
                    $series = [];
                    $series_g = [];
                    $series_d = [];
                    foreach ($juliDetail as $v) {
                        if (!empty($v['type_value'])) {
                            $categories[] = date('H:i', $v['taketime']);
                            $series[] = $v['type_value'];
                            $series_g[] = 100;
                            $series_d[] = 60;
                        }
                    }
                    if (!empty($series)) {
                        $userType = $userTypeModel::get(['pid'=>7,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '运动健康心率：（170-年龄）次/分钟';
                        $data[] = [
                            'title' => [
                                'text' => '心率(次/min)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $categories
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '心率',
                                    'data' =>
                                        $series,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准心率(最高)',
                                    'data' =>
                                        $series_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准心率(最低)',
                                    'data' =>
                                        $series_d,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '血压':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 21])->order('taketime asc')->select();

                    if (!empty($juliDetail)) {

//                        while (count($juliDetail) > 12 ) {
//                            foreach ($juliDetail as $key => $v) {
//                                if($key%2==0){
//                                    unset($juliDetail[$key]);
//                                }
//                            }
//                        }

                        foreach ($juliDetail as $v) {
                            $v_str = explode(',', $v['type_value']);
                            $tim[] = date('H:i', $v['taketime']);
                            $gao[] = $v_str[0];
                            $di[] = $v_str[1];
                            $gao_g[] = 139;
                            $gao_d[] = 90;
                            $di_g[] = 89;
                            $di_d[] = 60;
                        }
                    } else {
                        $tim = [0, 3, 6, 9, 12, 15, 18, 21, 24];
                        $gao = [0, 0, 0, 0, 0, 0, 0, 0, 0];
                        $di = [0, 0, 0, 0, 0, 0, 0, 0, 0];
                        $gao_g = [139, 139, 139, 139, 139, 139, 139, 139, 139];
                        $gao_d = [90, 90, 90, 90, 90, 90, 90, 90, 90];
                        $di_g = [89, 89, 89, 89, 89, 89, 89, 89, 89];
                        $di_d = [60, 60, 60, 60, 60, 60, 60, 60, 60];
                    }

                    if (!empty($tim)) {
                        $userType = $userTypeModel::get(['pid'=>4,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '成人：收缩压90～139mmHg和舒张压60～89mmHg';
                        $data[] = [
                            'title' => [
                                'text' => '血压(mmHg)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $tim
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '舒张压',
                                    'data' =>
                                        $di,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '舒张压(最高值)',
                                    'data' =>
                                        $di_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '舒张压(最低值)',
                                    'data' =>
                                        $di_d,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压',
                                    'data' =>
                                        $gao,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压(最高值)',
                                    'data' =>
                                        $gao_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压(最低值)',
                                    'data' =>
                                        $gao_d,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '卡路里':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 24])->order('taketime asc')->select();
                    $categories = [];
                    $series = [];
                    $ka = 0;
                    foreach ($juliDetail as $v) {
                        if (!empty($v['type_value'])) {
                            $ka += $v['type_value'];
                            $categories[] = date('H:i', $v['taketime']);
                            $series[] =  sprintf("%.2f", $ka);
                        }
                    }
                    if (!empty($series)) {
                        $userType = $userTypeModel::get(['pid'=>10,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '成人轻度体力劳动：2100-2400千卡；成人中度体力劳动：2300-2700千卡；成人重度体力劳动：2700-3200千卡';
                        $data[] = [
                            'title' => [
                                'text' => '卡路里(千卡)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $categories
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '卡路里',
                                    'data' =>
                                        $series,
                                    'type' => 'line'
                                ],
                            ]
                        ];
                    }

                    break;
                case '运动距离':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 12])->order('taketime asc')->select();
                    $categories = [];
                    $series = [];
                    $bu = 0;
                    foreach ($juliDetail as $v) {
                        if (!empty($v['type_value'])) {
                            $bu += $v['type_value'];
                            $categories[] = date('H:i', $v['taketime']);
                            $series[] = sprintf("%.2f", $bu);
                        }
                    }
                    if (!empty($series)) {
                        $userType = $userTypeModel::get(['pid'=>11,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '参考标准范围:≥3公里/日';
                        $data[] = [
                            'title' => [
                                'text' => '运动距离(公里)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $categories
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '运动距离',
                                    'data' =>
                                        $series,
                                    'type' => 'line',
                                    'color' => '#93CE07'
                                ],
                            ]
                        ];
                    }

                    break;
                case '睡眠':
                    $juliDetail = $recordModel->where(['user_id' => $user_id, 'day' => $day, 'type_id' => 17])->order('taketime asc')->select();
                    $categories = [];
                    $ruArr = [];
                    $qianArr = [];
                    $shenArr = [];
                    $qingArr = [];
                    $ru_num = 0;
                    $qian_num = 0;
                    $shen_num = 0;
                    $qing_num = 0;
                    foreach ($juliDetail as $k => $v) {
                        $check = 0;
                        //入睡时间
                        $ruDetail = $recordModel::get(['user_id' => $user_id, 'day' => $day, 'type_id' => 14, 'taketime' => $v['taketime']]);
                        if (!$ruDetail) {
                            $ruArr[] = 0;
                        } else {
                            $ruArr[] = 10;
                            $ru_num += 10;
                            $check++;
                        }
                        //浅睡时间
                        $qianDetail = $recordModel::get(['user_id' => $user_id, 'day' => $day, 'type_id' => 18, 'taketime' => $v['taketime']]);
                        if (!$qianDetail) {
                            $qianArr[] = 0;
                        } else {
                            $qianArr[] = 10;
                            $qian_num += 10;
                            $check++;
                        }
                        //深睡时间
                        $shenDetail = $recordModel::get(['user_id' => $user_id, 'day' => $day, 'type_id' => 19, 'taketime' => $v['taketime']]);
                        if (!$shenDetail) {
                            $shenArr[] = 0;
                        } else {
                            $shenArr[] = 10;
                            $shen_num += 10;
                            $check++;
                        }
                        //清醒睡眠
                        $qingDetail = $recordModel::get(['user_id' => $user_id, 'day' => $day, 'type_id' => 16, 'taketime' => $v['taketime']]);
                        if (!$qingDetail) {
                            $qingArr[] = 0;
                        } else {
                            $qingArr[] = 10;
                            $qing_num += 10;
                            $check++;
                        }
                        if ($check == 0) {
                            unset($ruArr[$k]);
                            unset($qianArr[$k]);
                            unset($shenArr[$k]);
                            unset($qingArr[$k]);
                            $checkDetail = $recordModel::get(['user_id' => $user_id, 'day' => $day, 'type_id' => ['in', [14, 16, 18, 19]], 'taketime' => ['>', $v['taketime']]]);
                            if (!$checkDetail) {
                                break;
                            }
                        } else {
                            $categories[] = date('H:i', $v['taketime']);
                        }

                    }
                    $ruArr = array_values($ruArr);
                    $qianArr = array_values($qianArr);
                    $shenArr = array_values($shenArr);
                    $qingArr = array_values($qingArr);

                    if($ru_num > 0){
                        $ru_h = sprintf("%.1f", ($ru_num / 60));
                    }else{
                        $ru_h = 0;
                    }

                    if($qian_num > 0){
                        $qian_h = sprintf("%.1f", ($qian_num / 60));
                    }else{
                        $qian_h = 0;
                    }

                    if($shen_num > 0){
                        $shen_h = sprintf("%.1f", ($shen_num / 60));
                    }else{
                        $shen_h = 0;
                    }

                    if($qing_num > 0){
                        $qing_h = sprintf("%.1f", ($qing_num / 60));
                    }else{
                        $qing_h = 0;
                    }
                    if (!empty($categories)) {
                        $userType = $userTypeModel::get(['pid'=>12,'type'=>0]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环(睡眠时长参考标准范围：7-8小时（正常成人))';
                        $data[] = [
                            'title' => [
                                'text' => $val . '(h)',
                                'subtext' => $text
                            ],
                            'legend' => [
                                'data' => [
                                    '开始入睡('.$ru_h.')',
                                    '浅睡('.$qian_h.')',
                                    '深睡('.$shen_h.')',
                                    '清醒睡眠('.$qing_h.')'
                                ]
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' => $categories
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' =>
                                        '开始入睡('.$ru_h.')',
                                    'type' => 'bar',
                                    'data' => $ruArr
                                ],
                                [
                                    'name' =>
                                        '浅睡('.$qian_h.')',
                                    'type' => 'bar',
                                    'data' => $qianArr
                                ],
                                [
                                    'name' =>
                                        '深睡('.$shen_h.')',
                                    'type' => 'bar',
                                    'data' => $shenArr
                                ],
                                [
                                    'name' =>
                                        '清醒睡眠('.$qing_h.')',
                                    'type' => 'bar',
                                    'data' => $qingArr
                                ],
                            ]
                        ];

//                        $data[] = [
//                            'title' => [
//                                'text' => '睡眠'
//                            ],
//                            'xAxis' => [
//                                'type' => 'category',
//                                'data' =>
//                                    $categories
//                            ],
//                            'yAxis' => [
//                                'type' => 'value'
//                            ],
//                            'series' => [
//                                [
//                                    'name' => '开始入睡',
//                                    'data' =>
//                                        $ruArr,
//                                    'type' => 'line'
//                                ],
//                                [
//                                    'name' => '浅睡',
//                                    'data' =>
//                                        $qianArr,
//                                    'type' => 'line'
//                                ],
//                                [
//                                    'name' => '深睡',
//                                    'data' =>
//                                        $shenArr,
//                                    'type' => 'line'
//                                ],
//                                [
//                                    'name' => '清醒睡眠',
//                                    'data' =>
//                                        $qingArr,
//                                    'type' => 'line'
//                                ]
//                            ]
//                        ];
                    }

                    break;
            }
        }

        $this->success('请求成功', $data);
    }

    public function weekRecord()
    {
        $day = input('post.day', date('Y-m', time()));

        $user_id = input('user_id');

        $type = input('post.type');

        $data = [];

        $day = strtotime($day);

        $week = $this->get_week($day);

        $weekNameArr = [];
        $weekTimeArr = [];
        foreach ($week as $val) {
            $weekNameArr[] = $val['week'];
            $weekTimeArr[] = $val['date'];
        }

        if (empty($type)) {
            $search = $this->week;
        } else {
            $search_arr = explode(',', $type);
            $search = [];
            $day = $this->week;
            foreach ($search_arr as $key => $val) {
                if ($val == 99) {
                    $search = $this->week;
                    break;
                }
                $search[$key] = $day[$val];
            }
        }
        $recordModel = new InfoTypeRecord();

        $userTypeModel = new UserTypeModel();

        $userAdviseData = new UserAdviseDataModel();
        foreach ($search as $val) {
            switch ($val) {
                case '入睡时间':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = $recordModel->where(['type_id' => 14, 'user_id' => $user_id, 'day' => $item])->count();
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = $detail * 10;
                        }
                    }

                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>13,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                        //标准饮水(参考)
                        $c_sleep_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>16]);
                        $c_sleep_d = !empty($c_sleep_info['num']) ? $c_sleep_info['num'] : 25;
                        $c_sleep_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>14]);
                        $c_sleep_g = !empty($c_sleep_info['num']) ? $c_sleep_info['num'] : 30;

                        $data[] = [
                            'title' => [
                                'text' => $val . '(min)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '标准线最高值',
                                    'data' =>
                                        [$c_sleep_g, $c_sleep_g, $c_sleep_g, $c_sleep_g, $c_sleep_g, $c_sleep_g, $c_sleep_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '均值',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准线最低值',
                                    'data' =>
                                        [$c_sleep_d, $c_sleep_d, $c_sleep_d, $c_sleep_d, $c_sleep_d, $c_sleep_d, $c_sleep_d],
                                    'type' => 'line'
                                ],
                            ]
                        ];
                    }

                    break;
                case '睡眠时间':
                    foreach ($weekTimeArr as $item) {
                        //入睡时间
                        $ru_detail = $recordModel->where(['type_id' => 14, 'user_id' => $user_id, 'day' => $item])->count();
                        //清醒时间
                        $qing_detail = $recordModel->where(['type_id' => 16, 'user_id' => $user_id, 'day' => $item])->count();
                        //浅睡
                        $qian_detail = $recordModel->where(['type_id' => 18, 'user_id' => $user_id, 'day' => $item])->count();
                        //深睡
                        $shen_detail = $recordModel->where(['type_id' => 19, 'user_id' => $user_id, 'day' => $item])->count();
                        if (empty($ru_detail)) {
                            $ru_list[] = 0;
                        } else {
                            $ru_list[] = sprintf("%.1f", $ru_detail * 10 / 60);
                        }
                        if (empty($qing_detail)) {
                            $qing_list[] = 0;
                        } else {
                            $qing_list[] = sprintf("%.1f", $qing_detail * 10 / 60);
                        }
                        if (empty($qian_detail)) {
                            $qian_list[] = 0;
                        } else {
                            $qian_list[] = sprintf("%.1f", $qian_detail * 10 / 60);;
                        }
                        if (empty($shen_detail)) {
                            $shen_list[] = 0;
                        } else {
                            $shen_list[] =  sprintf("%.1f", $shen_detail * 10 / 60);;
                        }
//                        $ju_detail = ($qian_detail + $shen_detail + $ru_detail) / 3;
//                        $ju_list[] = sprintf("%.2f", $ju_detail);
                    }

                    if (!empty($ru_list) || !empty($qing_list) || !empty($qian_detail) || !empty($shen_list)) {
                        $userType = $userTypeModel::get(['pid'=>1,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                        $data[] = [
                            'title' => [
                                'text' => $val . '(h)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '入睡时间',
                                    'data' =>
                                        $ru_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '清醒时间',
                                    'data' =>
                                        $qing_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '浅睡眠时间',
                                    'data' =>
                                        $qian_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '深睡时间',
                                    'data' =>
                                        $shen_list,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }
                    break;

                case '血压':
                    $d_list = [];
                    $g_list = [];
                    $gao_g = [];
                    $gao_d = [];
                    $di_g = [];
                    $di_d = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = $recordModel->where(['type_id' => 21, 'user_id' => $user_id, 'day' => $item])->select();

                        if (empty($detail)) {
                            $d_list[] = 0;
                            $g_list[] = 0;
                        } else {
                            $d_num = count($detail);
                            $d = 0;
                            $g = 0;
                            foreach ($detail as $v) {
                                $v_arr = explode(',', $v['type_value']);
                                $d += $v_arr[0];
                                $g += $v_arr[1];
                            }
                            $d = $d / $d_num;
                            $g = $g / $d_num;
                            $d_list[] = sprintf("%.0f", $d);
                            $g_list[] = sprintf("%.0f", $g);

                        }
                        $gao_g[] = 139;
                        $gao_d[] = 90;
                        $di_g[] = 89;
                        $di_d[] = 60;

                    }

                    $userType = $userTypeModel::get(['pid'=>3,'type'=>1]);

                    $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                    $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                    if (!empty($d_list) || !empty($g_list)) {
                        $data[] = [
                            'title' => [
                                'text' => $val . '(mmHg)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '舒张压',
                                    'data' =>
                                        $d_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压',
                                    'data' =>
                                        $g_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压(最高值)',
                                    'data' =>
                                        $gao_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '收缩压(最低值)',
                                    'data' =>
                                        $gao_d,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '舒张压(最高值)',
                                    'data' =>
                                        $di_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '舒张压(最低值)',
                                    'data' =>
                                        $di_d,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;

                case '血氧':
                    $detail_list = [];
                    $detail_g = [];
                    $detail_d = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = $recordModel->where(['type_id' => 7, 'user_id' => $user_id, 'day' => $item])->select();
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $d = 0;
                            $d_num = count($detail);
                            foreach ($detail as $v) {
                                $d += $v['type_value'];
                            }
                            $d = $d / $d_num;
                            $detail_list[] = sprintf("%.2f", $d);

                        }
                        $detail_g[] = 100;
                        $detail_d[] = 95;
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>4,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                        $data[] = [
                            'title' => [
                                'text' => $val . '(%)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '血氧值',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准(高)',
                                    'data' =>
                                        $detail_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准(低)',
                                    'data' =>
                                        $detail_d,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }
                    break;

                case '运动步数':
                    $detail_list = [];
                    $b_g = [];
                    $b_d = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = $recordModel->where(['type_id' => 17, 'user_id' => $user_id, 'day' => $item])->sum('type_value');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = $detail;
                        }
                        $b_g[] = 10000;
                        $b_d[] = 6000;
                    }

                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>5,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        $data[] = [
                            'title' => [
                                'text' => $val . '(步)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '运动步数',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准（最高）',
                                    'data' =>
                                        $b_g,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准（最低）',
                                    'data' =>
                                        $b_d,
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '总饮水量':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {

                        //$detail = UserFoodModel::where(['type' => 5, 'user_id' => $this->auth->id, 'day' => $item])->sum('water');
                        $water = \app\admin\model\UserFood::where(['day' => $item, 'user_id' => $this->auth->id, 'type' => 5])->column('id');
                        $detail = UserFoodAttrModel::where('pid', 'in', $water)->sum('food_value');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = $detail;
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>6,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        //标准饮水
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>23]);
                        $c_drink = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 500;

                        $data[] = [
                            'title' => [
                                'text' => $val . '(ML)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总饮水量',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准饮水量',
                                    'data' =>
                                        [$c_drink,$c_drink,$c_drink,$c_drink,$c_drink,$c_drink,$c_drink],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '大便次数':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {

                        $detail = $recordModel->where(['type_id' => 22, 'user_id' => $user_id, 'day' => $item])->value('type_value');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = $detail;
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>7,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                        //标准饮水
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>17]);
                        $c_drink = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 1;

                        $data[] = [
                            'title' => [
                                'text' => $val . '次',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '大便次数',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '标准次数',
                                    'data' =>
                                        [$c_drink,$c_drink,$c_drink,$c_drink,$c_drink,$c_drink,$c_drink],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '总热量':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = UserFoodModel::where(['user_id' => $user_id, 'day' => $item])->sum('hot');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = round($detail,1);
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>8,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        //标准总热量
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>18]);
                        $c_drink_d = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 2100;
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>24]);
                        $c_drink_g = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 2400;
                        $data[] = [
                            'title' => [
                                'text' => $val . '(kcal)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总热量',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总热量(高)',
                                    'data' =>
                                        [$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总热量(低)',
                                    'data' =>
                                        [$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '总蛋白质':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = UserFoodModel::where(['user_id' => $user_id, 'day' => $item])->sum('protein');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = round($detail,1);
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>9,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';

                        //标准蛋白质
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>19]);
                        $c_drink_d = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 2;
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>25]);
                        $c_drink_g = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 5;

                        $data[] = [
                            'title' => [
                                'text' => $val . '(g)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总蛋白质',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总蛋白质(高)',
                                    'data' =>
                                        [$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总蛋白质(低)',
                                    'data' =>
                                        [$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '总脂肪':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = UserFoodModel::where(['user_id' => $user_id, 'day' => $item])->sum('fat');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = round($detail,1);
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>10,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        //标准脂肪
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>20]);
                        $c_drink_d = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 12;
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>26]);
                        $c_drink_g = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 18;
                        $data[] = [
                            'title' => [
                                'text' => $val . '(g)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总脂肪',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总脂肪(高)',
                                    'data' =>
                                        [$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总脂肪(低)',
                                    'data' =>
                                        [$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
                case '总膳食纤维':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = UserFoodModel::where(['user_id' => $user_id, 'day' => $item])->sum('dietary');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = round($detail,1);
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>11,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        //标准膳食纤维
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>21]);
                        $c_drink_d = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 12;
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>27]);
                        $c_drink_g = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 18;
                        $data[] = [
                            'title' => [
                                'text' => $val . '(g)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总膳食纤维',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总膳食纤维(高)',
                                    'data' =>
                                        [$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总膳食纤维(低)',
                                    'data' =>
                                        [$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }


                    break;
                case '总碳水化合物':
                    $detail_list = [];
                    foreach ($weekTimeArr as $item) {
                        $detail = UserFoodModel::where(['user_id' => $user_id, 'type' => ['NEQ', 5], 'day' => $item])->sum('water');
                        if (empty($detail)) {
                            $detail_list[] = 0;
                        } else {
                            $detail_list[] = round($detail,1);
                        }
                    }
                    if (!empty($detail_list)) {
                        $userType = $userTypeModel::get(['pid'=>12,'type'=>1]);

                        $userAdvise = UserAdviseModel::get(['user_id'=>$user_id,'type_id'=>$userType['id']]);

                        $text = !empty($userAdvise['title']) ? $userAdvise['title'] : '数据来自蓝牙手环';
                        //标准碳水化合物
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>22]);
                        $c_drink_d = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 12;
                        $c_drink_info = $userAdviseData::get(['user_id'=>$user_id,'type_id'=>28]);
                        $c_drink_g = !empty($c_drink_info['num']) ? $c_drink_info['num'] : 18;
                        $data[] = [
                            'title' => [
                                'text' => $val . '(g)',
                                'subtext' => $text
                            ],
                            'xAxis' => [
                                'type' => 'category',
                                'data' =>
                                    $weekNameArr
                            ],
                            'yAxis' => [
                                'type' => 'value'
                            ],
                            'tooltip' => [
                                'trigger' => 'axis'
                            ],
                            'series' => [
                                [
                                    'name' => '总碳水化合物',
                                    'data' =>
                                        $detail_list,
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总碳水化合物(高)',
                                    'data' =>
                                        [$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g,$c_drink_g],
                                    'type' => 'line'
                                ],
                                [
                                    'name' => '总碳水化合物(低)',
                                    'data' =>
                                        [$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d,$c_drink_d],
                                    'type' => 'line'
                                ]
                            ]
                        ];
                    }

                    break;
            }

        }
        $this->success('请求成功', $data);

    }

    /**
     *  * 获取一周日期
     *  * @param $time 时间戳
     *  * @param $format 转换格式
     *  */
    public
    function get_week($time, $format = "Y-m-d")
    {
        $week = date('w', $time);
        $weekname = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');
        if (empty($week)) {
            $week = 7;
        }
        for ($i = 0; $i <= 6; $i++) {
            $data[$i]['date'] = date($format, strtotime('+' . $i + 1 - $week . ' days', $time));
            $data[$i]['week'] = $weekname[$i];
        }
        return $data;
    }
}